@echo off
chcp 65001 >nul
echo 🚀 HarmonyOS 构建脚本 (修复版)

echo.
echo 📦 构建 Ionic 应用...
call npm run build
if %errorlevel% neq 0 (
    echo ❌ 构建失败
    pause
    exit /b 1
)

echo.
echo 📁 复制文件到 HarmonyOS...
call npm run build:harmonyos
if %errorlevel% neq 0 (
    echo ❌ 文件复制失败
    pause
    exit /b 1
)

echo.
echo 🔨 构建 HarmonyOS 应用...
cd /d "%~dp0ohos"

echo 🧹 清理项目...
if exist "hvigorw.bat" (
    call hvigorw.bat clean
) else if exist "gradlew.bat" (
    call gradlew.bat clean
) else (
    echo ⚠ 未找到构建工具，请在DevEco Studio中手动构建
    cd /d "%~dp0"
    pause
    exit /b 1
)

echo 🔨 开始构建...
if exist "hvigorw.bat" (
    call hvigorw.bat assembleHap
    set build_result=%errorlevel%
) else if exist "gradlew.bat" (
    call gradlew.bat assembleHap
    set build_result=%errorlevel%
) else (
    echo ❌ 构建工具不可用
    cd /d "%~dp0"
    pause
    exit /b 1
)

cd /d "%~dp0"

if %build_result% neq 0 (
    echo ❌ HarmonyOS 构建失败
    pause
    exit /b 1
)

echo.
echo 🎉 构建完成！
echo 📱 HAP 文件: ohos\entry\build\default\outputs\default\entry-default-signed.hap
echo.
echo 📝 下一步操作：
echo 1. 在 DevEco Studio 中打开 ohos 目录
echo 2. 连接 HarmonyOS 设备或启动模拟器  
echo 3. 点击 Run 按钮运行应用
pause